﻿using System;
using System.Collections.Generic;
using System.Text;

namespace gChartSharp
{
    /// <summary>
    /// The base class from which all Charts are derived
    /// </summary>
    public abstract class ChartBase : IChart
    {
        /// <summary>
        /// 
        /// </summary>
        protected const string APIURL = @"http://chart.apis.google.com/chart?";


        /// <summary>
        /// 
        /// </summary>
        public abstract ChartType ChartType { get; }
        
        /// <summary>
        /// 
        /// </summary>
        public SizeParam Size
        {
            get
            {
                if (mSize == null)
                    mSize = new SizeParam();

                return mSize;
            }
            set { mSize = value; }
        }
        private SizeParam mSize;


        /// <summary>
        /// 
        /// </summary>
        public DataSeriesParam Data
        {
            get 
            {
                if (mData == null)
                    mData = new DataSeriesParam();

                return mData;
            }
            set { mData = value; }
        }
        private DataSeriesParam mData;

        /// <summary>
        /// The default constructor
        /// </summary>
        public ChartBase() { }


        /// <summary>
        /// The simplest chart constructor to initialize the minimum required
        /// properties to render all graphs.
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="series"></param>
        /// <param name="encType"></param>
        public ChartBase(int width, int height, EncodingType encType, float[] dataSets)
        {
            this.Size = new SizeParam(width, height);
            this.Data = new DataSeriesParam(dataSets);
            this.Data.EncodingType = encType;
        }


        /// <summary>
        /// Initializes a graph with no data sets
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="encType"></param>
        public ChartBase(int width, int height, EncodingType encType)
        {
            this.Size = new SizeParam(width, height);
            this.Data.EncodingType = encType;
        }


        /// <summary>
        /// Initializes a graph with no data sets
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public ChartBase(int width, int height) 
            : this(width, height, EncodingType.Text) { }


        /// <summary>
        /// Initializes a graph with one data set and default encoding
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="dataSet"></param>
        public ChartBase(int width, int height, float[] dataSet)
            : this(width, height, EncodingType.Text, dataSet) { }


        /// <summary>
        /// Creates the base API URL inside a StringBuilder object
        /// </summary>
        /// <returns></returns>
        protected StringBuilder CreateBaseURL()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(String.Format("{0}cht={1}{2}{3}", APIURL, this.ChartType.ToString(), this.Size.Render(), this.Data.Render()));
            return sb;
        }


        /// <summary>
        /// Renders a chart parameter
        /// </summary>
        /// <param name="param">A chart parameter</param>
        /// <param name="sb">A reference to a StringBuilder object</param>
        protected void RenderIChartParam(IChartParam param, ref StringBuilder sb)
        {
            if (param != null) { sb.Append(String.Format("{0}", param.Render())); }
        }

        public abstract string GetURL();

        public abstract string GetHTML();
    }
}
